/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.invocation.client;

import java.io.Serializable;
import java.util.Date;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
@SuppressWarnings("serial")
public class DataModel implements Referenceable, Serializable {

    protected ObjectRef ref;

    protected PropertyMap properties;

    protected DataModel() {
    }

    public DataModel(String ref) {
        this(ref, new PropertyMap());
    }

    public DataModel(String ref, PropertyMap properties) {
        this(new ObjectRef(ref), properties);
    }

    public DataModel(String type, String id) {
        this(type, id, new PropertyMap());
    }

    public DataModel(String type, String id, PropertyMap properties) {
        this(new ObjectRef(type, id), properties);
    }

    public DataModel(ObjectRef ref) {
        this(ref, new PropertyMap());
    }

    public DataModel(ObjectRef ref, PropertyMap properties) {
        this.ref = ref;
        this.properties = properties;
    }

    public void setProperties(PropertyMap properties) {
        this.properties.putAll(properties);
    }

    /**
     * The returned object is mutable.
     * 
     * @return
     */
    public PropertyMap getProperties() {
        return properties;
    }

    public String getString(String key) {
        return properties.getString(key);
    }

    public Boolean getBoolean(String key) {
        return properties.getBoolean(key);
    }

    public Long getLong(String key) {
        return properties.getLong(key);
    }

    public Date getDate(String key) {
        return properties.getDate(key);
    }

    public Serializable getProperty(String key) {
        return properties.get(key);
    }

    public void setProperty(String key, Serializable value) {
        properties.put(key, value);
    }

    @Override
    public ObjectRef getRef() {
        return ref;
    }

    public String getTitle() {
        String title = properties.getString("dc:title");
        return title == null || title.length() == 0 ? ref.getId() : title;
    }

    public void setTitle(String text) {
        properties.put("dc:title", text);
    }

    public String getIcon() {
        return null;
    }

    public boolean sameAs(Referenceable ref) {
        return this.ref.equals(ref.getRef());
    }

    @Override
    public String toString() {
        return ref.toString();
    }

}
